package com.scarecrow.olga.util.math {
	
	/**
	 * TRIG REMINDER
	 * ---------------------------------------------
	 * ANGLE BETWEEN OBJ "A" AND OBJ "B":
	 * 
	 * var distanceX:Number = (objectA.x-objectB.x);
	 * var distanceY:Number = (objectA.y-objectB.y);
	 * var angle:Number = Math.atan(distanceX, distanceY);
	 * 
	 * RESULT IS GIVEN IN RADIANS.
	 * ---------------------------------------------
	 * 
	 * ...
	 * @author Michael James Williams
	 * @author Santiago.J.Elizalde
	 */
	public class TrigUtil {
		
		private static const localPi:Number = Math.PI;
		private static const localTwoPi:Number = Math.PI * 2;
		private static const oneOver180:Number = 1 / 180;
		private static const oneOverPi:Number = 1 / localPi;
		
		public function TrigUtil() { }
		
		/**
		 *  Returns a degrees value, capped at a 0 to 360 range, regardless of the range of
		 * the degree value provided.
		 * 
		 * @param	degrees	Angle, in degrees
		 * @return	Angle, in degrees, in range 0 to 360
		 */
		public static function degFromDeg( degrees:Number ):Number {
			var degOutput:Number = degrees;
			while (degOutput >= 360) {
				degOutput -= 360;
			}
			while (degOutput < 0) {
				degOutput += 360;
			}
			return degOutput;
		}
 
		/**
		 * Returns a rotation value, capped at a -180 to 180 range, regardless of the range of
		 * the rotation value provided.
		 * 
		 * @param	rotation	Angle, in degrees
		 * @return	Angle, in degrees, in range -180 to + 180
		 */
		public static function rotFromRot( rotation:Number ):Number {
			var rotOutput:Number = rotation;
			while (rotOutput > 180) {
				rotOutput -= 360;
			}
			while (rotOutput < -180) {
				rotOutput += 360;
			}
			return rotOutput;
		}
 
		/**
		 *  Returns a radians value, capped at a 0 to ( 2 * pi ) range, regardless of the range of
		 * the radians value provided.
		 * 
		 * @param	radians	Angle, in radians
		 * @return	Angle, in radians, in range 0 to ( 2 * pi )
		 */
		public static function radFromRad( radians:Number ):Number {
			var radOutput:Number = radians;
			while (radOutput >= localTwoPi) {
				radOutput -= localTwoPi;
			}
			while (radOutput < -localTwoPi) {
				radOutput += localTwoPi;
			}
			return radOutput;
		}
 
		/**
		 * @param	degrees	Angle, in degrees
		 * @return	Angle, in degrees, in range -180 to +180
		 */
		public static function rotFromDeg( degrees:Number ):Number {
			var rotOutput:Number = degrees;
			while (rotOutput > 180) {
				rotOutput -= 360;
			}
			while (rotOutput < -180) {
				rotOutput += 360;
			}
			return rotOutput;
		}
 
		/**
		 * @param	rotation	Angle, in degrees
		 * @return	Angle, in degrees, in range 0 to 360
		 */
		public static function degFromRot( rotation:Number ):Number {
			var degOutput:Number = rotation;
			while (degOutput >= 360) {
				degOutput -= 360;
			}
			while (degOutput < 0) {
				degOutput += 360;
			}
			return degOutput;
		}
 
		/**
		 * @param	radians	Angle, in radians
		 * @return	Angle, in degrees, in range 0 to 360
		 */
		public static function degFromRad( radians:Number ):Number {
			var degOutput:Number = 180 * oneOverPi * radFromRad(radians);
			return degOutput;
		}
 
		/**
		 * @param	degrees	Angle, in degrees
		 * @return	Angle, in radians, in range 0 to ( 2 * pi )
		 */
		public static function radFromDeg( degrees:Number ):Number {
			var radOutput:Number = localPi * oneOver180 * degFromDeg(degrees);
			return radOutput;
		}
 
		/**
		 * @param	radians	Angle, in radians
		 * @return	Angle, in degrees, in range -180 to +180
		 */
		public static function rotFromRad(radians:Number):Number {
			return rotFromDeg(degFromRad(radians));
		}
 
		/**
		 * @param	rotation	Angle, in degrees
		 * @return	Angle, in radians, in range 0 to ( 2 * pi )
		 */
		public static function radFromRot(rotation:Number ):Number {
			return radFromDeg(degFromRot(rotation));
		}
	}

}